package org.ms.springboot.elasticsearch;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.rest.RestStatus;
import org.junit.Test;

/**
 * 索引操作
 *
 * @author lry
 */
public class ElasticsearchIndex extends ElasticsearchBase {

    /**
     * 创建索引
     */
    @Test
    public void createIndex() {
        IndexRequest request = new IndexRequest("posts", "doc", "1").source(jsonMap);
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        //Operation type provided as a String: can be CREATE or INDEX (default)
        request.opType(DocWriteRequest.OpType.CREATE);
        request.timeout("1s");

        try {
            IndexResponse indexResponse = client.index(request);
            System.out.println("IndexResponse: " + indexResponse);
            if (DocWriteResponse.Result.CREATED == indexResponse.getResult()) {
                System.out.println("创建索引成功：" + indexResponse.getResult());
            } else {
                System.out.println("创建索引失败：" + indexResponse.getResult());
            }
        } catch (ElasticsearchException e) {
            // If there is a version conflict, an ElasticsearchException will be thrown
            if (e.status() == RestStatus.CONFLICT) {
                System.out.printf("版本冲突:" + RestStatus.CONFLICT);
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建或更新索引
     */
    @Test
    public void createOrUpdateIndex() {
        IndexRequest request = new IndexRequest("posts", "doc", "1").source(jsonMap);
        request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
        request.opType(DocWriteRequest.OpType.INDEX);
        request.timeout("1s");

        try {
            IndexResponse indexResponse = client.index(request);
            System.out.println("IndexResponse: " + indexResponse);
            if (DocWriteResponse.Result.CREATED == indexResponse.getResult()) {
                System.out.println("创建索引成功：" + indexResponse.getResult());
            } else if (DocWriteResponse.Result.UPDATED == indexResponse.getResult()) {
                System.out.println("更新索引成功：" + indexResponse.getResult());
            } else {
                System.out.println("创建或更新索引失败：" + indexResponse.getResult());
            }
        } catch (ElasticsearchException e) {
            if (e.status() == RestStatus.CONFLICT) {
                System.out.printf("版本冲突");
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除索引
     */
    @Test
    public void deleteIndex() {
        DeleteIndexRequest request = new DeleteIndexRequest("posts");
        request.timeout("2m");

        try {
            DeleteIndexResponse response = client.indices().deleteIndex(request);
            System.out.println("DeleteIndexResponse: " + response.isAcknowledged());
        } catch (ElasticsearchException e) {
            if (e.status() == RestStatus.NOT_FOUND) {
                System.out.println("没有发现索引");
            } else {
                e.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
